<?php
/**
 * @version 	$Id: fSmartyEngine.class.php 74 2010-03-17 06:48:21Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access');
require_once _VENDORS_PATH_.'smarty'._DS_.'Smarty.class.php';
//fLoader::loadVendor('smarty', 'smarty', 'class.php');

/**
 * Flywheel Smarty Engine 
 * 	Template Engine su dung vendor Smarty
 * 
 * @package 	system
 * @subpackage 	template
 *
 */
class fSmartyEngine extends Smarty {
	private $name = 'default';
	public function __construct() {
		parent::__construct ();
		$this->init();
	}
	
	public function setName($s) {
		$this->name = $s;		
	}
	
	private function init() {						
		if (!fConfig::has('smarty', 'template')) {			
			fConfig::load(_APP_CONFIGURATION_PATH_.'template.ini', 'template');
						 
		}
		$config = fConfig::get('smarty', 'template');
		
		$this->compile_check = (boolean) $config['compile_check'];
		$this->force_compile = (boolean) $config['force_compile'];		
		$this->caching = (int) $config['caching'];
		$this->cache_lifetime = (int) $config['cache_lifetime'];
		
		//Set template dir	
		$this->template_dir = _APP_TEMPLATES_PATH_.'smarty'._DS_.$this->name._DS_.'templates'._DS_;
		$this->compile_dir = _APP_TEMPLATES_PATH_.'smarty'._DS_.$this->name._DS_.'templates_c'._DS_;
		$this->cache_dir = _ROOT_PATH_.'cache'._DS_.fFactory::getApplication()->getName()._DS_.'temp'._DS_;
		
		//Init template name
		$config = array_merge($config, fConfig::get('template'));
		$this->setName($config['name']);				
		
		if (fConfig::get('debug') && $config['debug']) {
			$this->debugging = true;
			$this->debug_tpl = $this->template_dir.'debug.tpl';
		}
	}
	
	/**
	 * Render
	 *
	 * @param string $layout
	 * @return HTML String
	 */
	public function render($view) {
		$this->createPath($this->template_dir.$view);
		$this->createPath($this->compile_dir);
		return $this->fetch($view .'.tpl', $_SERVER['REQUEST_URI'], null, false);						
	}
	
	/**
	 * Display
	 * 	overight method display on Smarty class
	 *  
	 * @param string $view
	 */
	public function display($view) {
		$this->createPath($this->template_dir.$view);
		$this->createPath($this->compile_dir);
		parent::display($view.'.tpl');		
	}
	
	/**
	 * Create Template Path
	 *
	 * @param string $path
	 * @return void
	 */
	private function createPath($path) {		
		$tempArr = explode(_DS_, $path);
		array_pop($tempArr);
		$templateDir = implode(_DS_, $tempArr);
		fFolderHelper::create($templateDir);
	}
	
	public function clearAllAssign() {
		$this->clear_all_assign();
	}
}